package com.demo.crm.controller;

import com.arvato.crm.model.entity.DataDict;
import com.arvato.crm.service.DataDictService;
import com.demo.crm.model.AjaxInfo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author tanshuai
 */
@Controller
@RequestMapping("/dict")
public class DataDictController {

    @RequestMapping("/category/{name}")
    @ResponseBody
    public List listByCategory(@PathVariable String name) {
        List<DataDict> dictList = DataDictService.getListByCategory(name);
        if (dictList == null) {
            return Collections.emptyList();
        }
        Collections.sort(dictList, new Comparator<DataDict>() {
            public int compare(DataDict o1, DataDict o2) {
                return o1.getPriority() - o2.getPriority();
            }
        });
        return dictList;
    }

    @RequestMapping("/getText/{category}/{value}")
    @ResponseBody
    public String getText(@PathVariable String category, @PathVariable String value) {
        return DataDictService.getTextWithDefault(category, value);
    }

    @RequestMapping("/refresh")
    @ResponseBody
    public AjaxInfo refresh() {
        DataDictService.refresh();
        return AjaxInfo.SUCCESS;
    }
}
